package com.ruoyi.web.controller.community.advert;

import java.io.IOException;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.AdResource;
import com.ruoyi.community.service.IAdResourceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 广告资源Controller
 * 
 * @author ruoyi
 * @date 2021-08-23
 */
@RestController
@RequestMapping("/advert/resource")
public class AdResourceController extends BaseController
{
    @Autowired
    private IAdResourceService adResourceService;

    /**
     * 查询广告资源列表
     */
    @PreAuthorize("@ss.hasPermi('advert:resource:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdResource adResource)
    {
        startPage();
        List<AdResource> list = adResourceService.selectAdResourceList(adResource);
        return getDataTable(list);
    }

    /**
     * 导出广告资源列表
     */
    @PreAuthorize("@ss.hasPermi('advert:resource:export')")
    @Log(title = "广告资源", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AdResource adResource)
    {
        List<AdResource> list = adResourceService.selectAdResourceList(adResource);
        ExcelUtil<AdResource> util = new ExcelUtil<AdResource>(AdResource.class);
        return util.exportExcel(list, "广告资源数据");
    }

    /**
     * 获取广告资源详细信息
     */
    @PreAuthorize("@ss.hasPermi('advert:resource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(adResourceService.selectAdResourceById(id));
    }

    /**
     * 新增广告资源
     */
    @PreAuthorize("@ss.hasPermi('advert:resource:add')")
    @Log(title = "广告资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdResource adResource)
    {
        return toAjax(adResourceService.insertAdResource(adResource));
    }

    /**
     * 修改广告资源
     */
    @PreAuthorize("@ss.hasPermi('advert:resource:edit')")
    @Log(title = "广告资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdResource adResource)
    {
        return toAjax(adResourceService.updateAdResource(adResource));
    }

    /**
     * 删除广告资源
     */
    @PreAuthorize("@ss.hasPermi('advert:resource:remove')")
    @Log(title = "广告资源", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(adResourceService.deleteAdResourceByIds(ids));
    }

    @PostMapping("/picture")
    public AjaxResult uploadPicture(@RequestParam("file")MultipartFile file) throws IOException {
        String name = adResourceService.uploadPicture(file);
        if (name == null) {
            return AjaxResult.error("没有接受到图片请重新上传");
        } else if ("error".equals(name)) {
            return AjaxResult.error("上传出错请重新上传");
        }
        return AjaxResult.success("上传成功", name);
    }

    // 删除图片
    @DeleteMapping("/picture")
    public AjaxResult delPicture(@PathVariable String name) {
        String res = adResourceService.delPicture(name);
        return "success".equals(res) ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");

    }
}
